package com.sysu.homework.action;

import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionSupport;
import com.sysu.homework.service.CourseService;
import com.sysu.homework.service.StudentService;
import com.sysu.homework.service.TeachAssistantService;
import com.sysu.homework.service.TeacherService;

@Component("loginAction")
@Scope("prototype")
public class LoginAction extends ActionSupport{
	private String role;
	private int id;
	private String password;
	private StudentService studentService;
	private CourseService courseService;
	private TeachAssistantService teachAssistantService;
	private TeacherService teacherService;
	
	@Override
	public String execute() throws Exception {
		
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/javascript;charset=GBK");
		PrintWriter out = response.getWriter();
		
		if((role.equals("student")&&studentService.isValid(id, password))
				|| (role.equals("teacher")&&teacherService.isVaild(id, password))
				|| (role.equals("ta")&&teachAssistantService.isVaild(id, password))){
			HttpSession session = ServletActionContext.getRequest().getSession();;
			
			session.setAttribute("user", id);
			session.setAttribute("role", role);
			out.println(1);
		}else {
			//System.out.println(2);
			out.println(2);
		}
		
		return null;
	}
	
	public CourseService getCourseService() {
		return courseService;
	}

	@Resource
	public void setCourseService(CourseService courseService) {
		this.courseService = courseService;
	}


	public TeachAssistantService getTeachAssistantService() {
		return teachAssistantService;
	}

	@Resource
	public void setTeachAssistantService(TeachAssistantService teachAssistantService) {
		this.teachAssistantService = teachAssistantService;
	}


	public TeacherService getTeacherService() {
		return teacherService;
	}

	@Resource
	public void setTeacherService(TeacherService teacherService) {
		this.teacherService = teacherService;
	}


	public int getId() {
		return id;
	}


	public void setId(int id) {
		this.id = id;
	}


	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Resource
	public void setStudentService(StudentService studentService) {
		this.studentService = studentService;
	}


	public StudentService getStudentService() {
		return studentService;
	}
	
	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}

	
}
